package net.keebook.webapp.ws;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import net.keebook.webapp.dao.UserDao;
import net.keebook.webapp.entity.GeneratorPreferences;
import net.keebook.webapp.entity.User;
import net.keebook.webapp.model.SessionModel;

import org.json.JSONException;
import org.json.JSONObject;


@Path("/savegeneratorsettings")
@Stateless
public class SaveGeneratorSettingsService {
	@Inject
	private UserDao userDao;
	
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public String saveSettings(@Context HttpServletRequest request, String jsonString) throws JSONException {
		HttpSession session = request.getSession(true);
		SessionModel sessionModel = SessionModel.getInstance(session);
		
		JSONObject json = new JSONObject(jsonString);
		
		JSONObject result = new JSONObject();
		User user = getUserDao().get(sessionModel.getUserId());
		if (user == null) {
			result.put("success", false);
		} else {
			GeneratorPreferences preferences;
			if (user.getGeneratorPreferences() == null) {
				preferences = new GeneratorPreferences();
				getUserDao().store(preferences);
				user.setGeneratorPreferences(preferences);
			} else {
				preferences = user.getGeneratorPreferences();
			}
			preferences.setLength(16);
			preferences.setBrackets(false);
			preferences.setDigits(false);
			preferences.setHighAnsi(false);
			preferences.setLowerCase(false);
			preferences.setMinus(false);
			preferences.setSpace(false);
			preferences.setSpecial(false);
			preferences.setUnderline(false);
			preferences.setUpperCase(false);
			for (String name : JSONObject.getNames(json)) {
				if ("length".equals(name)) {
					preferences.setLength(json.getInt(name));
				} else if ("upperCase".equals(name)) {
					preferences.setUpperCase(json.getBoolean(name));
				} else if ("space".equals(name)) {
					preferences.setSpace(json.getBoolean(name));
				} else if ("lowerCase".equals(name)) {
					preferences.setLowerCase(json.getBoolean(name));
				} else if ("special".equals(name)) {
					preferences.setSpecial(json.getBoolean(name));
				} else if ("digits".equals(name)) {
					preferences.setDigits(json.getBoolean(name));
				} else if ("brackets".equals(name)) {
					preferences.setBrackets(json.getBoolean(name));
				} else if ("minus".equals(name)) {
					preferences.setMinus(json.getBoolean(name));
				} else if ("highAnsi".equals(name)) {
					preferences.setHighAnsi(json.getBoolean(name));
				} else if ("underline".equals(name)) {
					preferences.setUnderline(json.getBoolean(name));
				}
			}
			getUserDao().update(user);
			result.put("success", true);
		}
		return result.toString();
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}
